﻿using System;
using System.Web;
using MySql.Data.MySqlClient;

namespace MyHTTPModule
{
    /// <summary>
    /// Summary description for MyHTTPModule
    /// </summary>
    public class MyHTTPModule : IHttpModule
    {
        static readonly string _connStr = String.Format("server={0};uid={1};pwd={2};database={3};Allow User Variables=True;",
                                                      "anotherurl.com", "root", "rootpw!", "laoslog");

        /*
         
         
drop database laoslog;
create database laoslog;
use laoslog;
         
create table hits(hit_id int NOT NULL AUTO_INCREMENT,
time DateTime,
ip varchar(100),
url varchar(255),
ua varchar(100),
PRIMARY KEY (hit_id)
);         
         
         * */

        private static void ApplicationBeginRequest(Object source, EventArgs e)
        {
            var webRequest = source as HttpApplication;
            if (webRequest == null)
                return;
      
            if (webRequest.Request.RawUrl.Contains("maptile.aspx"))
                return;

            if (webRequest.Request.RawUrl.EndsWith("sqlquery.cshtml"))
                return;

            if (webRequest.Request.RawUrl.EndsWith(".js"))
                return;
            if (webRequest.Request.RawUrl.EndsWith(".css"))
                return;
            if (webRequest.Request.RawUrl.EndsWith(".png"))
                return;
            if (webRequest.Request.RawUrl.EndsWith(".jpg"))
                return;
            if (webRequest.Request.RawUrl.EndsWith(".gif"))
                return;
            if (webRequest.Request.RawUrl.EndsWith(".ico"))
                return;
            if (webRequest.Request.RawUrl.EndsWith("robots.txt"))
                return;

            if (webRequest.Request.RawUrl.EndsWith(".php"))
                return; // remove annoying php hack attempts.

            string ua = webRequest.Request.UserAgent ?? "";
            
            if (ua.Contains("Baiduspider"))
                return;
            if (ua.Contains("Microsoft-WebDAV"))
                return;
            if (ua.Contains("Googlebot"))
                return;
            if (ua.Contains("crawler@alexa.com"))
                return;
            if (ua.Contains("ezooms.bot@gmail.com"))
                return;
            if (ua.Contains("www.domaincrawler.com"))
                return;
            if (ua.Contains("http://yandex.com/bots"))
                return;
            if (ua.Contains("www.80legs.com"))
                return;

            if (webRequest.Request.UserHostAddress == "127.0.0.1")
                return;

            SubmitReport(webRequest.Request.UserHostAddress, webRequest.Request.RawUrl, ua);
        }

        private static void SubmitReport(string ip, string url, string ua)
        {
            string time = String.Format("{0:yyyy-M-dd HH:mm:ss}", DateTime.UtcNow);

            try
            {
                string sql = "INSERT INTO hits(time, ip, url, ua) VALUES(\"" + time + "\", \"" + ip + "\", \"" + url + "\", \"" + ua + "\");";

                using (var conn = new MySqlConnection(_connStr))
                {
                    conn.Open();

                    using (var cmd = new MySqlCommand(sql, conn))
                    {
                        cmd.ExecuteNonQuery();
                    }

                    conn.Close();
                }
            }
            catch (Exception)
            {
            }
        }

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += ApplicationBeginRequest;
        }
    }
}